package com.youku.util;

import java.security.MessageDigest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 加密工具
 * 
 * @author loda
 *
 */
public class EncryptHelper {

	private static final Logger logger = LoggerFactory.getLogger(EncryptHelper.class);

	public static String md5(String src) {
		byte[] bytes;
		try {
			bytes = MessageDigest.getInstance("MD5").digest(src.getBytes());
			return byte2hex(bytes).toUpperCase();
		} catch (Exception e) {
			logger.error("error when md5:", e);
			return null;
		}
	}

	private static String byte2hex(byte abyte0[]) {
		StringBuilder sb = new StringBuilder(abyte0.length * 2);
		for (int i = 0; i < abyte0.length; i++) {
			if ((abyte0[i] & 0xff) < 16) {
				sb.append("0");
			}
			sb.append(Long.toString((long) abyte0[i] & (long) 255, 16));
		}

		return sb.toString();
	}

}
